import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_common_app/utils/common_util/log_util.dart';
import 'package:flutter_common_app/widgets/custom_image/custom_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RotatingPicture extends StatefulWidget {
  final AudioModel model;

  const RotatingPicture(this.model, {Key key}) : super(key: key);

  @override
  _RotatingPictureState createState() => _RotatingPictureState();
}

class _RotatingPictureState extends State<RotatingPicture>
    with TickerProviderStateMixin {
  AnimationController _controller; // 封面旋转控制器

  @override
  void initState() {
    _controller =
        AnimationController(vsync: this, duration: Duration(seconds: 20));
    _controller.addStatusListener((status) {
      // 转完一圈之后继续
      if (status == AnimationStatus.completed) {
        _controller.reset();
        _controller.forward();
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if(widget.model.playerState == AudioPlayerState.PLAYING){
      _controller.forward();
    }else{
      _controller.stop();
    }
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(10000)),
        border: Border.all(
          color: Colors.white.withOpacity(0.2),
          width: 15.w,
        ),
      ),
      child: RotationTransition(
        turns: _controller,
        child: CustomImage.network(
          '${widget.model.currentSongData.picUrl}?param=500y500',
          borderRadius: BorderRadius.all(
            Radius.circular(1000),
          ),
          width: 500.w,
          height: 500.w,
        ),
      ),
    );
  }
}
